<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2015/8/2
 * Time: 10:01
 */

class admin extends adminBase{
    function  __construct(){
        parent::__construct();
        $this->cms->setPageTitle("管理员管理");
        $this->cms->setControlFile("admin/tpl/admin/admin_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(16,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page){
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $user=AdminModule::init('admin');
        list($r['list'],$admin_total)=$user->getList($countOfPage,$offset);
        $r['levels']=array("3"=>"商家管理","4"=>"服务站管理","5"=>"会员管理",
            "6"=>"商品管理","7"=>"评价管理","8"=>"标签管理","9"=>"订单管理","10"=>"消息管理",
            "11"=>"销售管理","12"=>"退货管理","13"=>"财务管理","14"=>"文章管理","15"=>"广告管理","16"=>"管理员管理","17"=>"地址管理");
        $url=WebRouter::init()->getQuestion("page=");
        $r['pages']=new Page($page,$admin_total,$countOfPage);
        $r['pages']->setPageCallback(function($page)use($url){
            return $url.$page;
        });
        $this->cms->tableScene($r,"admin/tpl/admin/admin.php");

    }

    function addTask(){
        $r['levels']=array("3"=>"商家管理","4"=>"服务站管理","5"=>"会员管理",
            "6"=>"商品管理","7"=>"评价管理","8"=>"标签管理","9"=>"订单管理","10"=>"消息管理",
            "11"=>"销售管理","12"=>"退货管理","13"=>"财务管理","14"=>"文章管理","15"=>"广告管理","16"=>"管理员管理","17"=>"地址管理");
        $this->cms->formScene($r,"admin/tpl/admin/admin_add.php");
    }

    function addSubmitTask($username,$email,$password,$levels){
        $user = AdminModule::init('admin');
        if($user->getIdByUsername($username)){
            $r['message']="用户名已存在";
        }else{
            if($this->user->checkTelFormatValid($username)){
                $code=getPassWord(trim($username),trim($password));
                if($user->adminAdd(trim($username),$email,$code,implode(",",$levels))){
                    $r['message']="添加成功";
                    $r['url']=WebRouter::init()->getAction("index");
                }else{
                    $r['message']="添加失败，请重试！";
                }
            }else{
                $r['message']="用户名格式不正确！";
            }
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function modifyTask($id){
        $r['levels']=array("3"=>"商家管理","4"=>"服务站管理","5"=>"会员管理",
            "6"=>"商品管理","7"=>"评价管理","8"=>"标签管理","9"=>"订单管理","10"=>"消息管理",
            "11"=>"销售管理","12"=>"退货管理","13"=>"财务管理","14"=>"文章管理","15"=>"广告管理","16"=>"管理员管理","17"=>"地址管理");
        $user = UserMode::init();
        $r['admin_info']=$user->getAdminDetail($id);
        $r['id']=$id;
        $this->cms->formScene($r,"admin/tpl/admin/admin_modify.php");
    }

    function modifySubmitTask($id,$levels){
        $user=UserMode::init();
        if($user->adminModify($id,implode(",",$levels))){
            $r['message']="修改成功";
            $r['url']=WebRouter::init()->getAction("index");
        }else{
            $r['message']="修改失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function deleteTask($id){
        $user=UserMode::init();
        if($user->adminDelete($id)){
            $r['message']="删除成功";
            $r['url']=WebRouter::init()->getAction("index");
        }else{
            $r['message']="删除失败，请重试！";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
}